import logging

from absl import app

import coin.proto.account_control_pb2 as control_proto
from coin.proto.coin_request_pb2 import AccountRequestProto
from coin.support.account_control.app.extra_balance_converter import (BalanceConversionController,
                                                                      BalanceConversionStatus,
                                                                      _collect_balance,
                                                                      _dump_result,
                                                                      _start_controller)
from xunkemgmt.support.accounting.logic.query_request import AccountRequest


def main(argv):
  dry_run = True
  balance_request_list = [
      AccountRequest(market_type='Spot', exchange='Okex', owner='prestoinvt04'),
      AccountRequest(market_type='Spot', exchange='Okex', owner='prestoinvt04')
  ]
  init_balance_dict = _collect_balance(balance_request_list)
  controller_list = []
  control_request = control_proto.ControlRequest(
      target=AccountRequestProto(market_type='Spot', exchange='Okex', owner='prestoinvt04'),
      symbol='BTC-USDT',
      type=control_proto.CONTROL_APPROACH_POSITION,
      approach_position=control_proto.ApproachPositionRequest(target_position=0.03,
                                                              target_error_tolerance=0.1,
                                                              pass_bp=2,
                                                              lot_size=0.01,
                                                              stickiness=0,
                                                              order_update_period=2))
  controller_list.append(
      BalanceConversionController('test',
                                  control_request,
                                  '../../coin_key_high/trade/prestoinvt04/OKEx/trade_v3_key.json',
                                  dry_run))
  control_request = control_proto.ControlRequest(
      target=AccountRequestProto(market_type='Spot', exchange='Okex', owner='prestoinvt04'),
      symbol='BTC-USDT',
      type=control_proto.CONTROL_APPROACH_POSITION,
      approach_position=control_proto.ApproachPositionRequest(target_position=0.075,
                                                              target_error_tolerance=0.1,
                                                              pass_bp=2,
                                                              lot_size=0.01,
                                                              stickiness=0,
                                                              order_update_period=2))
  controller_list.append(
      BalanceConversionController('test',
                                  control_request,
                                  '../../coin_key_high/trade/prestoinvt04/OKEx/trade_v3_key.json',
                                  dry_run))
  slack_receiver = 'hft_raw_noti'
  _dump_result(controller_list, init_balance_dict, init_balance_dict, slack_receiver)
  _start_controller(balance_request_list, controller_list, init_balance_dict, slack_receiver)


if __name__ == '__main__':
  logging.basicConfig(level='DEBUG', format='%(levelname)s %(asctime)s %(name)s] %(message)s')

  app.run(main)
